/**
 * 用于跨页面通讯
 */
var base = require("lib/comp/base"); // 基础对象
var addEvent = require("lib/evt/add");
var md5 = require("lib/util/md5");
var each = require("lib/util/each");
var moduleId = "lib/io/postMessager";
var listeners = {
    'all': []
};

addEvent(window, "message", function(ev) {
    var data = null;
    var evObj = null;

    try {
        data = JSON.parse(ev.data);
    } catch(ex) {
        console.error(ex);
        return;
    }

    if (data["moduleId"] != moduleId) {
        return;
    }

    each(listeners['all'], function(listener) {
        if (listener["target"].getKey() == data["targetKey"]) {
            return;
        }

        evObj = {
            "target": listener["target"],
            "data": data.msg,
            "fromKey": data["targetKey"]
        }

        try {
            listener["target"].getHandler()(evObj);
        }catch(ex) {
            console.error(ex);
        }
    });

    if (listeners[data["toKey"]]) {
        evObj = {
            "target": listeners[data["toKey"]]["target"],
            "data": data.msg,
            "fromKey": data["targetKey"]
        }

        try {
            listeners[data["toKey"]]["target"].getHandler()(evObj);
        }catch(ex) {
            console.error(ex);
        }
    }
});

/**
 * postMessager的管理对象
 * @param  {[type]}  handler [数据回调函数]
 * @param  {[type]}  domain  [发送目标域名，默认为*]
 * @param  {Boolean} bindAll   [接收所有使用本组件发送的消息，默认仅接收发送给本组件的消息]
 * @return {[type]}          [description]
 */
module.exports = function(handler, bindAll, domain) {
    var that = {};
    var key = md5((new Date().getTime() + Math.floor(Math.random() * 100000)).toString());
    bindAll = bindAll === true ? true : false;
    domain = domain || "*";

    var listener = {
        "target": that,
        "handler": handler
    }

    if (bindAll) {
        listeners['all'].push(listener);
    } else {
        listeners[key] = listener;
    }

    that.send = function(data, toKey) {
        var data = {
            "moduleId": moduleId,
            "targetKey": key,
            "toKey": toKey,
            "msg": data
        }

        postMessage(JSON.stringify(data), domain);
    }

    that.getKey = function() {
        return key;
    }

    that.getDomain = function() {
        return domain;
    }

    that.bindAll = function() {
        return bindAll;
    }

    that.getHandler = function() {
        return handler;
    }

    return that;
}